home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kgame / kgameprocess.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  8.0 KB  |  243 lines

  1. /*
  2.     This file is part of the KDE games library
  3.     Copyright (C) 2001 Martin Heni (martin@heni-online.de)
  4.     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License version 2 as published by the Free Software Foundation.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public License
  16.     along with this library; see the file COPYING.LIB.  If not, write to
  17.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.     Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21.     $Id: kgameprocess.h 465369 2005-09-29 14:33:08Z mueller $
  22. */
  23. #ifndef __KGAMEPROCESS_H_
  24. #define __KGAMEPROCESS_H_
  25.  
  26. #include <qstring.h>
  27. #include <qobject.h>
  28. #include <qfile.h>
  29.  
  30. #include "kgameproperty.h"
  31. #include <krandomsequence.h>
  32. #include <kdemacros.h>
  33. class KPlayer;
  34. class KMessageFilePipe;
  35.  
  36. /**
  37.  * This is the process class used on the computer player
  38.  * side to communicate with its counterpart KProcessIO class.
  39.  * Using these two classes will give fully transparent communication
  40.  * via QDataStreams.
  41.  */
  42. class KDE_EXPORT KGameProcess:  public QObject
  43. {
  44.   Q_OBJECT
  45.  
  46.   public:
  47.     /**
  48.      * Creates a KGameProcess class. Done only in the computer
  49.      * player. To activate the communication you have to call
  50.      * the exec function of this class which will listen
  51.      * to the communication and emit signals to notify you of
  52.      * any incoming messages.
  53.      * Note: This function will only return after you set
  54.      * setTerminate(true) in one of the received signals.
  55.      * So you can not do any computer calculation after the exec function.
  56.      * Instead you react on the signals which are emitted after a
  57.      * message is received and perform the calculations there!
  58.      * Example:
  59.      * \code
  60.      *  int main(int argc ,char * argv[])
  61.      *  {
  62.      *    KGameProcess proc;
  63.      *    connect(&proc,SIGNAL(signalCommand(QDataStream &,int ,int ,int )),
  64.      *                    this,SLOT(slotCommand(QDataStream & ,int ,int ,int )));
  65.      *    connect(&proc,SIGNAL(signalInit(QDataStream &,int)),
  66.      *                    this,SLOT(slotInit(QDataStream & ,int )));
  67.      *    connect(&proc,SIGNAL(signalTurn(QDataStream &,bool )),
  68.      *                    this,SLOT(slotTurn(QDataStream & ,bool )));
  69.      *    return proc.exec(argc,argv);
  70.      *  }
  71.      *  \endcode
  72.      */
  73.     KGameProcess();
  74.     /**
  75.      * Destruct the process
  76.      */
  77.     ~KGameProcess();
  78.  
  79.     /**
  80.      * Enters the event loop of the computer process. Does only
  81.      * return on setTerminate(true)!
  82.      */
  83.     bool exec(int argc, char *argv[]);
  84.  
  85.     /**
  86.      * Should the computer process leave its exec function?
  87.      * Activated if you setTerminate(true);
  88.      *
  89.      * @return true/false
  90.      */
  91.     bool terminate() const {return mTerminate;}
  92.  
  93.     /**
  94.      * Set this to true if the computer process should end, ie
  95.      * leave its exec function.
  96.      *
  97.      * @param b true for exit the exec function
  98.      */
  99.     void setTerminate(bool b) {mTerminate=b;}
  100.  
  101.     /**
  102.      * Sends a message to the corresponding KGameIO
  103.      * device. Works like the sendSystemMessage but
  104.      * for user id's
  105.      *
  106.      * @param stream the QDataStream containing the message
  107.      * @param msgid the message id for the message
  108.      * @param receiver unused
  109.      */
  110.     void sendMessage(QDataStream &stream,int msgid,Q_UINT32 receiver=0);
  111.  
  112.     /**
  113.      * Sends a system message to the corresonding KGameIO device.
  114.      * This will normally be either a performed move or a query
  115.      * (IdProcessQuery). The query option is a way to communicate
  116.      * with the KGameIO at the other side and e.g. retrieve some
  117.      * game relevant data from here.
  118.      * Exmaple for a query:
  119.      * \code
  120.      *  QByteArray buffer;
  121.      *  QDataStream out(buffer,IO_WriteOnly);
  122.      *  int msgid=KGameMessage::IdProcessQuery;
  123.      *  out << (int)1;
  124.      *  proc.sendSystemMessage(out,msgid,0);
  125.      * \endcode
  126.      *
  127.      * @param stream the QDataStream containing the message
  128.      * @param msgid the message id for the message
  129.      * @param receiver unused
  130.      */
  131.     void sendSystemMessage(QDataStream &stream,int msgid,Q_UINT32 receiver=0);
  132.  
  133.     /**
  134.      * Returns a pointer to a KRandomSequence. You can generate
  135.      * random numbers via e.g.
  136.      * \code
  137.      *   random()->getLong(100);
  138.      * \endcode
  139.      * 
  140.      * @return KRandomSequence pointer
  141.      */
  142.     KRandomSequence *random() {return mRandom;}
  143.  
  144.   protected:
  145.     /**
  146.      * processes the command line argumens to set up the computer player
  147.      * Pass the argumens exactely as given by main()
  148.      */
  149.     void processArgs(int argc, char *argv[]);
  150.  
  151.   protected slots:
  152.     /**
  153.      * A message is received via the interprocess connection. The
  154.      * appropriate signals are called.
  155.      */
  156.       void receivedMessage(const QByteArray& receiveBuffer);
  157.  
  158.   signals:
  159.     /**
  160.      * The generic communication signal. You have to connect to this
  161.      * signal to generate a valid computer response onto arbitrary messages.
  162.      * All signals but IdIOAdded and IdTurn end up here!
  163.      * Example:
  164.      * \code
  165.      * void Computer::slotCommand(int &msgid,QDataStream &in,QDataStream &out)
  166.      * {
  167.      *   Q_INT32 data,move;
  168.      *   in >> data;
  169.      *   // compute move ...
  170.      *   move=data*2;
  171.      *   out << move;
  172.      * }
  173.      * \endcode
  174.      *
  175.      * @param inputStream the incoming data stream
  176.      * @param msgid the message id of the message which got transmitted to the computer
  177.      * @param receiver the id of the receiver
  178.      * @param sender the id of the sender
  179.      */
  180.      void signalCommand(QDataStream &inputStream,int msgid,int receiver,int sender);
  181.  
  182.      /**
  183.       * This signal is emmited if the computer player should perform a turn.
  184.       * Calculations can be made here and the move can then be send back with
  185.       * sendSystemMessage with the message id KGameMessage::IdPlayerInput.
  186.       * These must provide a move which complies to your other move syntax as
  187.       * e.g. produces by keyboard or mouse input.
  188.       * Additonal data which have been written into the stream from the
  189.       * ProcessIO's signal signalPrepareTurn can be retrieved from the
  190.       * stream here.
  191.       * Example:
  192.       * \code
  193.       * void slotTurn(QDataStream &in,bool turn)
  194.       * {
  195.       *   int id;
  196.       *   int recv;
  197.       *   QByteArray buffer;
  198.       *   QDataStream out(buffer,IO_WriteOnly);
  199.       *   if (turn)
  200.       *   {
  201.       *     // Create a move - the format is yours to decide
  202.       *     // It arrives exactly as this in the kgame inputMove function!!
  203.       *     Q_INT8 x1,y1,pl;
  204.       *     pl=-1;
  205.       *     x1=proc.random()->getLong(8);
  206.       *     y1=proc.random()->getLong(8);
  207.       *     // Stream it
  208.       *     out << pl << x1 << y1;
  209.       *     id=KGameMessage::IdPlayerInput;
  210.       *     proc.sendSystemMessage(out,id,0);
  211.       *   }
  212.       * }
  213.       * \endcode
  214.       *
  215.       * @param stream The datastream which contains user data
  216.       * @param turn True or false whether the turn is activated or deactivated
  217.       *
  218.       */
  219.      void signalTurn(QDataStream &stream,bool turn);
  220.  
  221.      /**
  222.       * This signal is emmited when the process is initialized, i.e. added
  223.       * to a KPlayer. Initial initialisation can be performed here be reacting
  224.       * to the KProcessIO signal signalIOAdded and retrieving the data here
  225.       * from the stream. 
  226.       * It works just as the signalTurn() but is only send when the player is
  227.       * added to the game, i.e. it needs some initialization data
  228.       *
  229.       * @param stream The datastream which contains user data 
  230.       * @param userid The userId of the player. (Careful to rely on it yet)
  231.       */
  232.      void signalInit(QDataStream &stream,int userid);
  233.  
  234.   protected:
  235.     bool mTerminate;
  236.     KMessageFilePipe *mMessageIO;
  237.   private:
  238.     QFile rFile;
  239.     QFile wFile;
  240.     KRandomSequence* mRandom;
  241. };
  242. #endif
  243.